/**
 * Answer.java
 * com.zhihu.zhihuquan.model
 * Copyright (c) 2016, 北京聚智未来科技有限公司版权所有.
*/

package com.zhihu.zhihuquan.model;

import java.util.List;

import com.eova.common.base.BaseModel;

/**
 * 首页，知乎圈
 * @author   wxl
 * @Date	 2016年12月23日 	 
 */
public class Answer extends BaseModel<Answer> {

	/**
	 * TODO（用一句话描述这个变量表示什么）
	 */

	private static final long serialVersionUID = 2130370312784262532L;

	public static final Answer dao = new Answer();

	/**
	 * 查询主页列表
	 */
	public List<Answer> queryAll() {
		String sql = "select  u.nickname,p.title,a.content,a.createDate,a.agree,a.oppose,a.nohelp,a.browse "
				+ "from c_user u inner join z_answer a on u.id=a.userid inner join z_problem p on a.problemid=p.id";
		return this.find(sql);
	}

	/**
	 * 首页查询 or u.nickName LIKE '%?%'
	 */
	public List<Answer> queryConditionList(String text) {
		String sql = "SELECT u.nickname as nickname,p.title as title,a.content as content,a.createDate as createDate,a.agree as agree,a.oppose as oppose,a.nohelp as nohelp,a.browse as browse  "
				+ "FROM c_user u inner join z_answer a on u.id=a.userid inner join z_problem p on a.problemid=p.id "
				+ "WHERE p.title like '%" + text + "%' or u.nickname like '%" + text + "%'";
		return this.find(sql);
	}

	/**
	 * 回答列表
	 * @param id
	 */
	public List<Answer> queryAnswerList(String id) {
		String sql = "SELECT "
				+ "(select u.nickname from c_user u where u.id=a.userId) as username,"
				+ "(select u.headImgUrl from c_user u where u.id=a.userId) as headimgurl,"
				+ "a.content,a.agree,a.browse,a.createDate FROM z_answer a WHERE a.problemId = ? ORDER BY a.agree DESC ";
		return this.find(sql, id);

	}

	/**
	 * 回答详情
	 * @param 回答aid
	 * 
	 */
	public Answer queryAnswer(String aid) {
		String sql = "SELECT (SELECT p.title FROM z_problem p WHERE p.id=a.problemid) as ptitle,"
				+ "(SELECT u.headimgurl from c_user u WHERE u.id=a.userid) as headimgurl,"
				+ "(SELECT u.nickname from c_user u WHERE u.id=a.userid) as answername,"
				+ "(SELECT p.content FROM z_problem p WHERE p.id=a.problemid) as pcontent,a.*"
				+ "FROM z_answer a WHERE a.id=?";
		return this.findFirst(sql, aid);

	}

}
